package com.bawei.erp.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.bawei.common.StockException;
import com.bawei.erp.service.StockService;
import com.bawei.util.JsonUtil;

@RestController
@RequestMapping("/stock")
public class StockController {

	@Resource
	private StockService stockService;

	@RequestMapping("/getStock")
	public String getStock() {
		return JsonUtil.success(stockService.getStock());
	}

	@RequestMapping("/stockIn")
	public String stockIn(String sku, String count, String type) {
		try {
			stockService.stockIn(sku, Integer.parseInt(count), Byte.valueOf(type));
		} catch (StockException e) {
			return JsonUtil.error(e.getMessage());
		}
		return JsonUtil.success();
	}

	@RequestMapping("/stockOut")
	public String stockOut(String sku, String count, String type) {
		try {
			stockService.stockOut(sku, Integer.parseInt(count), Byte.valueOf(type));
		} catch (StockException e) {
			return JsonUtil.error(e.getMessage());
		}
		return JsonUtil.success();
	}

	@RequestMapping("/stockLock")
	public String stockLock(String sku) {
		stockService.lock(sku);
		return JsonUtil.success();
	}

	@RequestMapping("/stockUnLock")
	public String stockUnLock(String sku) {
		stockService.unLock(sku);
		return JsonUtil.success();
	}
}
